package com.me.jingdongshop.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.me.common.base.Global;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.Cart;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ldl on 2017/5/5.
 */

public class ShopcartAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<List<Cart.CartBean>> cartBeenData;

    private Map<String, Object> item = new HashMap<>();         // 一个item的所有信息集合（item）
    private List<Map<String, Object>> list = new ArrayList<>(); // 所有商品所有信息集合（ListView）

    private int num;    // 商品数量

    public ShopcartAdapter(Context context,List<List<Cart.CartBean>> data) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        cartBeenData = data;
    }

    static class ViewHolder {
        public ImageView ivNoGoods; // 售空
        public ImageView ivCheck;   // 是否选中
        public ImageView ivGoodsImg;   // 商品图片
        public TextView tvName;     // 商品名称
        public TextView tvSpec;     // 商品规格
        public ImageView ivNumReduce;    // 数量减
        public EditText etNum;      // 商品数量
        public ImageView ivNumAdd;        // 数量加
        public TextView tvPriceCurrent;    // 当前价格
        public TextView tvPricePrevious;   // 原先价格
        public ImageButton ibExpand;        // 折叠
    }

    @Override
    public int getCount() {
        return cartBeenData==null?0:cartBeenData.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.fragment_shopcart_item, parent, false);
            holder.ivNoGoods = (ImageView) convertView.findViewById(R.id.iv_nogoods);
            holder.ivCheck = (ImageView) convertView.findViewById(R.id.iv_check);
            holder.ivGoodsImg = (ImageView) convertView.findViewById(R.id.iv_goods_img);
            holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tvSpec = (TextView) convertView.findViewById(R.id.tv_spec);
            holder.ivNumReduce = (ImageView) convertView.findViewById(R.id.iv_num_reduce);
            holder.etNum = (EditText) convertView.findViewById(R.id.et_num);
            holder.ivNumAdd = (ImageView) convertView.findViewById(R.id.iv_num_add);
            holder.tvPriceCurrent = (TextView) convertView.findViewById(R.id.tv_price_current);
            holder.tvPricePrevious = (TextView) convertView.findViewById(R.id.tv_price_previous);
            holder.ibExpand = (ImageButton) convertView.findViewById(R.id.ib_expand);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Cart.CartBean cartBean = cartBeenData.get(position).get(0);
        holder.ivNoGoods.setImageResource(R.drawable.no_goods);
        holder.ivCheck.setImageResource(R.drawable.nocheck);
        Picasso.with(Global.mContext).load(IHttpService.HOST_URL+cartBean.getProduct().getPic())
                .into(holder.ivGoodsImg);
//        holder.ivGoodsImg.setImageResource(R.drawable.app_logo);
//        holder.tvName.setText("秋冬新款男装毛呢西装男士休闲西服外套商务青年韩版修身小西装。");
        holder.tvName.setText(cartBean.getProduct().getName());
        holder.tvSpec.setText("颜色：灰色；尺码：L");
        // holder.ivNumReduce.setBackgroundResource(R.drawable.shopping_cart_product_num_reduce);
        holder.etNum.setBackgroundResource(R.drawable.shopping_cart_product_num_edit);
        holder.etNum.setText(""+cartBean.getProdNum());
        // holder.ivNumAdd.setBackgroundResource(R.drawable.shopping_cart_product_num_add);
        holder.tvPriceCurrent.setText("￥"+cartBean.getProduct().getPrice());
        holder.tvPricePrevious.setText("￥"+cartBean.getProduct().getPrice());
        holder.tvPricePrevious.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);    // 设置文字中划线
        holder.ibExpand.setBackgroundResource(R.drawable.expand);

        holder.ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "点击选中" + getItem(position) , Toast.LENGTH_SHORT).show();
                holder.ivCheck.setImageResource(R.drawable.ischeck);
            }
        });

        num = Integer.parseInt(holder.etNum.getText().toString());
        holder.ivNumReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (num <= 0) {
                    Toast.makeText(context, "数量为0", Toast.LENGTH_SHORT).show();
                    holder.etNum.setText(0 + "");
                    holder.ivNumReduce.setBackgroundResource(R.drawable.shopping_cart_product_num_reduce_disable);
                } else {
                    Toast.makeText(context, "数量减少", Toast.LENGTH_SHORT).show();
                    num = num - 1;
                    holder.etNum.setText(num + "");
                }
            }
        });

        holder.ivNumAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "数量增加", Toast.LENGTH_SHORT).show();
                num = num + 1;
                holder.etNum.setText(num + "");
                holder.ivNumReduce.setBackgroundResource(R.drawable.shopcart_num_reduce_selector);
            }
        });

        holder.ibExpand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "展开" + getItem(position), Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }
}